package com.yp.lession73.slice;

import com.yp.lession73.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.TouchEvent;

public class Touch2AbilitySlice extends AbilitySlice {
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_touch2);

        DirectionalLayout layout = (DirectionalLayout) findComponentById(ResourceTable.Id_fu_dir);
        //绑定触摸事件
        layout.setTouchEventListener(new Component.TouchEventListener() {

            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                int action = touchEvent.getAction();
                Text text = (Text) findComponentById(ResourceTable.Id_touch_text);
                MmiPoint pp = touchEvent.getPointerPosition(0);
//                float x=0;
//                float y=0;
                float startX=0;
                float startY=0;
                if(action == TouchEvent.PRIMARY_POINT_DOWN){
//                    x = pp.getX();
//                    y = pp.getY();
//                    text.setText("手指按上去了: x:"+x+"y:"+y);
                    startX = pp.getX();
                    startY = pp.getY();
                }else if (action == TouchEvent.POINT_MOVE){
//                    x = pp.getX();
//                    y = pp.getY();
//                    text.setText("手指在移动: x:"+x+"y:"+y);
                }else if (action == TouchEvent.PRIMARY_POINT_UP){
//                    x = pp.getX();
//                    y = pp.getY();
//                    text.setText("手指离开了: x:"+x+"y:"+y);
                    float endX = pp.getX();
                    float endY = pp.getY();
                    if (endX>startX){
                        text.setText("向右");
                    }else if (endX<startX){
                        text.setText("想做");
                    }else if (endX>startY){
                        text.setText("向下");
                    }else if (endY<startY){
                        text.setText("向上");
                    }
                }
                return true;
            }
        });
    }
}
